package com.zheng.cms.rocketmq.listener;



import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class WithdrawMessageListener implements com.aliyun.openservices.ons.api.MessageListener {

    private static final Logger log = LoggerFactory.getLogger(WithdrawMessageListener.class);



    public Action consume(Message msg, ConsumeContext context) {
        System.out.println("Receive: " + msg);
        String body = "";
        try {
            body = new String(msg.getBody(), "UTF-8");
            //这里传入body参数进行业务操作

            System.out.println(body);
            return Action.CommitMessage;
        } catch (Exception e) {
            log.error("消费订单号为:" + body + "的消息失败，错误详情为：" + e);
            int delay = msg.getReconsumeTimes();
            if (delay > 4) {
                // 重试次数超过3次丢弃此条消息 log.warn("消费消息重试次数过多，不再消费：" + body); return Action.CommitMessage; }
                // 消费失败
                return Action.CommitMessage;
            }
        }

        return Action.ReconsumeLater;
    }


}